package y2016.date1203.pm.古诗文网采集;

import okhttp3.OkHttpClient;
import y2016.date1203.pm.古诗文网采集.Caijier.Holder;

import java.util.Arrays;
import java.util.List;

public class CaijiGo {
    private Caijier mCaijiXml;
    private HttpString mHttpString;
    private boolean downingFlag = false;

    public CaijiGo(OkHttpClient okHttpClient) {
        mCaijiXml = new Caijier();
        mHttpString = new HttpString(okHttpClient);
    }

    public void go() {

        final MainWindow window = new MainWindow("古诗文网采集");
        window.callBack(new CallBack() {

            @Override
            public void backInfo(CaiInfo caiInfo) {
                System.out.println(caiInfo.toString());
                window.setLoadText("正在下载...");
                if (downingFlag == false) {
                    downingFlag = true;
                    writeText(caiInfo);
                    downingFlag = false;
                    window.setLoadText("下载完成...");
                }

            }
        });

    }

    private void writeText(CaiInfo caiInfo) {
        String path = caiInfo.getPath();
        if (TextUtil.isEmpty(path)) {
            path = "C:/Users/LuTao/Desktop/史记/采集.txt";
        }
//		String url = "http://so.gushiwen.org/guwen/book_26.aspx";
        String url = caiInfo.getUrl();
        if (TextUtil.isEmpty(caiInfo.getUrl())) {
            return;
        }
//		List<Holder> holderList = mCaijiXml.getHolderList(path, Caijier.MODE_PARENT,"gbk");
        List<Holder> holderList = mCaijiXml.getHolderList(url, Caijier.MODE_HTML, "gbk");

        String firstText = caiInfo.getFirstText();
        if (!TextUtil.isEmpty(firstText)) {
            TextTool.writeTextToFile(firstText, null, path, "gbk");
        }
//		

//		String[] delete = new String[]{"作者：庄子及门徒"};
        String[] delete = caiInfo.getDeletes();
        mHttpString.writeHtml(holderList, path, delete, "gbk");

        String lastText = caiInfo.getLastText();
        if (!TextUtil.isEmpty(lastText)) {
            TextTool.writeTextToFile(lastText, null, path, "gbk");
        }
    }

    public static class CaiInfo {
        private String path;
        private String url;
        private String[] deletes;
        private String firstText;
        private String lastText;

        public CaiInfo() {
        }

        public CaiInfo(String path, String url, String[] deletes,
                       String firstText, String lastText) {
            super();
            this.path = path;
            this.url = url;
            this.deletes = deletes;
            this.firstText = firstText;
            this.lastText = lastText;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String[] getDeletes() {
            return deletes;
        }

        public void setDeletes(String[] deletes) {
            this.deletes = deletes;
        }

        public String getFirstText() {
            return firstText;
        }

        public void setFirstText(String firstText) {
            this.firstText = firstText;
        }

        public String getLastText() {
            return lastText;
        }

        public void setLastText(String lastText) {
            this.lastText = lastText;
        }

        @Override
        public String toString() {
            return "CaiInfo [path=" + path + ", url=" + url + ", deletes="
                    + Arrays.toString(deletes) + ", firstText=" + firstText
                    + ", lastText=" + lastText + "]";
        }

    }
}

